<?php
namespace app\admin\controller;

use app\index\model\Test;
use think\Request;
use app\admin\tool\Parser;
use think\Db;


/**
 * 测试增删查改的一个类
 * 后台的基础功能 图片上传 多图上传
 * 五大常用方法
 * 更多的可以参考exhibition的控制器
 * Class Ceshi
 * @package app\admin\controller
 */
class Ceshi extends Base {

    public function lists(){

        $test = Test::where("1 = 1")->paginate(1);
        $this->data['model'] = $test;
        $this->data['column'] = $this->column();
        return view('',$this->data);
    }


    /**
     * @param Request $request
     * @return \think\response\View
     */
    public function add(Request $request){

        return view();
    }


    /**
     * @param Request $request
     * @return \think\response\View
     */
    public function edit(Request $request){

        return view();
    }


    /**
     * 新增和编辑一起处理 为了权限控制 不适合一起处理 还是分开比较好
     * @param Request $request
     */
    public function handle(Request $request){
        $data = $request->post();
        $validate = Loader::validate('Exhibition');
        if(!$validate->check($data)){
            $this->error($validate->getError());
        }

        $data['op_user'] = $this->admin->id;
        if(empty($data['id'])){
            $data['add_time'] = $data['update_time'] = time();
            $model = new ExhibitionModel();
            $model->data($data);
            if($model->allowField(true)->save()){
                $this->success("新增成功");
            }
        }else{
            $data['update_time'] = time();
            $model = new ExhibitionModel();
            if($model ->allowField(true)->save($data,['id'=>$data['id']])){
                $this->success("更新成功");
            }
        }

        $this->error("操作失败");
    }


    public function delete(Request $request){
        $model = Test::get($request->param('id'));
        if($model){
            $res = $model ->delete();
            if($res){
                $this->success("删除成功");
            }
        }

        $this->error("操作失败");
    }



    public function upload(Request $request){
        // 获取表单上传文件 例如上传了001.jpg

        $file = $request->file('file');
        $data = [];
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                // 成功上传后 获取上传信息
                $data['ext'] =  $info->getExtension();
                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                $data['name'] = $info->getSaveName();
                // 输出 42a79759f284b767dfcb2a0197904287.jpg
                $data["filename"] =  $info->getFilename();
                $data['url'] = "/uploads".DS.$info->getSaveName();
            }else{
                // 上传失败获取错误信息
                $data['error'] = $file->getError();
            }
        }
        return json_encode($data);
    }


    public function ueditor(){

        return view();
    }


    public function advanceList(){

        $this->data['title'] = '高级列表模板';
        $this->data['column'] = [
            'name'=>['name'=>'名称','fun'=>'text','options'=>''],
            'add_time'=>['name'=>'添加时间','fun'=>'datetime','options'=>['format'=>'Y-m-d']]
        ];
        $this->data['model'] = Test::where([])->paginate(10);
        $this->data['parser'] = new Parser();

        return view('',$this->data);
    }


    protected function column(){
        return [
            "name"=>"名称",
            "add_time"=>"加入时间"
        ];
    }


    public function insert(){
        //$res = Db::table('test')->insert(['name'=>'test','add_time'=>time()]);
        //$res = Db::table('test')->insertGetId(['name'=>'haha','add_time'=>time()]);
        $res = Db::table('test')->where(['name'=>'haha'])->find();
        var_dump($res);
    }


    public function tree(){
        $this->data['title'] = '测试tree';
        return view('',$this->data);
    }


    /**
     * 具有弹出框的操作
     */
    public function modal(){

        $this->data['title'] = '弹出框测试';
        return view('',$this->data);
    }



}
